<?php 

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();
 
jimport( 'joomla.application.component.model' );

class ShopModelOrders extends JModel
{
    
    var $_data;
	
	function getOrders()
    {
		$db =& JFactory::getDBO();
		$status = & JRequest::getVar('status');
		$user_id = JRequest::getInt('user_id',0);
		if($user_id > 0) // получаем все заказы сделанные пользователем
			$query = "SELECT * FROM #__shop_orders WHERE ownerid=$user_id ORDER BY id DESC";
		else
			switch($status){
			
			case 'done': // все выполненные заказы
				$query = "SELECT * FROM #__shop_orders WHERE status = 'Заказ выполнен' ORDER BY id DESC";
				break;
			case 'in_progress':
				$query = "SELECT * FROM #__shop_orders WHERE status = 'Заказ обрабатывается' ORDER BY id DESC";
				break;
			case 'waiting':
				$query = "SELECT * FROM #__shop_orders WHERE status = 'Ожидает обработки' ORDER BY id DESC";
				break;
			case 'warehouse':
				$query = "SELECT * FROM #__shop_orders WHERE status = 'Заказ на складе' ORDER BY id DESC";
				break;
			case 'bad':
				$query = "SELECT * FROM #__shop_orders WHERE status = 'Не состоялся' ORDER BY id DESC";
				break;
			default: // все заказы отсортированные по id
				$query = "SELECT * FROM #__shop_orders ORDER BY id DESC";
				break;
			}
		$db->setQuery($query);
		$res = $db->loadObjectList();
		return $res;			
		
    }
	
	function getOrder($id)
    {
		$db =& JFactory::getDBO();		
		$query = "SELECT * FROM #__shop_orders WHERE id=".$id." ";
		$db->setQuery($query);
		$res = $db->loadObject();
		
		return $res;			
		
    }
   
	
	function delete($id)
	{	
		if (!$id || !is_array($id)) {
            return false;
        }
		$query = 'SELECT * FROM #__shop_orders WHERE id IN ('.implode(',', $id).')';
		$this->_db->setQuery($query);
		if ($this->_db->loadResult() != null)
		{
			$goods = $this->_db->loadObjectList();
			
			$query = 'DELETE FROM #__shop_orders WHERE id IN ('.implode(',', $id).')';
			$this->_db->setQuery($query);
			$this->_db->query();
			return true;
		}
		return false;
	}
	
}
?>